package com.t235.gc.device.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.t235.gc.device.dao.DeviceFaultRecordDao;
import com.t235.gc.device.entity.Device;
import com.t235.gc.device.entity.DeviceFaultRecord;
import com.t235.gc.device.service.DeviceFaultRecordService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * 设备故障记录表(DeviceFaultRecord)表服务实现类
 *
 * @author makejava
 * @since 2021-03-24 08:58:30
 */
@Service("deviceFaultRecordService")
public class DeviceFaultRecordServiceImpl implements DeviceFaultRecordService {
    @Resource
    private DeviceFaultRecordDao deviceFaultRecordDao;

    /**
     * 通过ID查询单条数据
     *
     * @param recordid 主键
     * @return 实例对象
     */
    @Override
    public DeviceFaultRecord queryById(Long recordid) {
        return this.deviceFaultRecordDao.queryById(recordid);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<DeviceFaultRecord> queryAllByLimit(int offset, int limit) {
        return this.deviceFaultRecordDao.queryAllByLimit(offset, limit);
    }
    /**
     * 通过实体作为筛选条件查询
     *
     * @param deviceFaultRecord 实例对象
     * @return 对象列表
     */
    @Override
    public List<DeviceFaultRecord> queryAll(DeviceFaultRecord deviceFaultRecord) {
        return this.deviceFaultRecordDao.queryAll(deviceFaultRecord);
    }

    /**
     * 新增数据
     *
     * @param deviceFaultRecord 实例对象
     * @return 实例对象
     */
    @Override
    public DeviceFaultRecord insert(DeviceFaultRecord deviceFaultRecord) {
        this.deviceFaultRecordDao.insert(deviceFaultRecord);
        return deviceFaultRecord;
    }

    /**
     * 修改数据
     *
     * @param deviceFaultRecord 实例对象
     * @return 实例对象
     */
    @Override
    public DeviceFaultRecord update(DeviceFaultRecord deviceFaultRecord) {
        this.deviceFaultRecordDao.update(deviceFaultRecord);
        return this.queryById(deviceFaultRecord.getRecordid());
    }

    /**
     * 通过主键删除数据
     *
     * @param recordid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long recordid) {
        return this.deviceFaultRecordDao.deleteById(recordid) > 0;
    }

    public void exportExcel(DeviceFaultRecord deviceFaultRecord, HttpServletResponse response) {
        //获取数据库数据
        List<DeviceFaultRecord> deviceList = deviceFaultRecordDao.queryAll(deviceFaultRecord);
        deviceList.forEach(user->System.out.println(user));
        //生成Excel文件
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("设备故障表","设备故障列表"),
                Device.class, deviceList);
        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-Type", "application/vnd.ms-excel");
        //通过输出流输出文件
        try {
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + URLEncoder.encode("设备故障", "UTF-8") + ".xls");
            ServletOutputStream out = response.getOutputStream();
            workbook.write(out);
            out.flush();
            out.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
